package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

// 2.7.8
@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {


    @Override
    public boolean importOrderSettings(List<String[]> readExcel) {
        //    List  数据  批量录入  表  t_ordersetting 里面  mp   100ws
           List<OrderSetting> orderSettingList = changeTO(readExcel);
           //   orderSettingList  集合里面数据  数据库中是否存在 相关记录 如果存在    OrderSetting .setId(3)
           //  根据日期 查询  有无 重复
        for (OrderSetting orderSetting : orderSettingList) {
            Date orderDate = orderSetting.getOrderDate();
            OrderSetting isOrderSettingExist = baseMapper.findOrderSettingByOrderDate(orderDate);
            if(isOrderSettingExist!=null){
                orderSetting.setId(isOrderSettingExist.getId());
            }
        }
        // saveOrUpdateBatch(List<T>)
        boolean flag = saveOrUpdateBatch(orderSettingList,100);
        return flag;
    }

    @Override
    public Map findSettingData(int year, int month) {
        //   根据 传递年和月  加载 预约数据   map   日期  yyyy-MM-dd      value  {number:xxx,reservations:xxx}
         String beginDate = year+"-"+month+"-1";
         String endDate = year+"-"+month+"-31";
         List<OrderSetting> orderSettingList = baseMapper.findSettingListByDate(beginDate,endDate);
         Map map = changeData(orderSettingList);
        return map;
    }

    @Override
    public void updateNumberByOrderDate(int number, String orderdate) {
        baseMapper.updateNumberByOrderDate(number,orderdate);
    }

    @Override
    public int isOrderOkByOrderDate(String orderdate) {
        return baseMapper.isOrderOkByOrderDate(orderdate);
    }

    @Override
    @LcnTransaction
    public void updateReservationsByOrderDate(String orderdate) {
        baseMapper.updateReservationsByOrderDate(orderdate);
    }

    //  5.8

    private Map changeData(List<OrderSetting> orderSettingList) {
        Map map  = new HashMap();
        for (OrderSetting orderSetting : orderSettingList) {
            Date orderDate = orderSetting.getOrderDate();
            String date = DateUtils.parseDate2String(orderDate,"yyyy-MM-dd");
            map.put(date,orderSetting);
        }
        return  map;
    }

    private List<OrderSetting> changeTO(List<String[]> readExcel) {
        List<OrderSetting> orderSettingList = new ArrayList<>();
          if(readExcel!=null&&readExcel.size()!=0){
              for (String[] strings : readExcel) {
                  OrderSetting orderSetting = new OrderSetting();
                  orderSetting.setReservations(0);
                  orderSetting.setNumber(Integer.parseInt(strings[1]));
                  orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0],"yyyy/MM/dd"));
                  orderSettingList.add(orderSetting);
              }
          }
        return  orderSettingList;
    }
}
